package com.example.yaja.yaja.controller;

import com.example.yaja.yaja.common.BaseResponse;
import com.example.yaja.yaja.common.ResponseBuilder;
import com.example.yaja.yaja.dao.NewsMapper;
import com.example.yaja.yaja.entity.Idea;
import com.example.yaja.yaja.entity.News;
import com.example.yaja.yaja.service.NewsService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.util.*;

/**
 * Created by Zero on 2017/12/4.
 */
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    NewsService newsService;
    @Autowired
    private NewsMapper newsMapper;

    /**
     * 查找新闻信息
     * @return
     */
    @RequestMapping(value = "/findNews",method = RequestMethod.GET)
    public Map<String,Object> findNews(){

        Map<String,Object> map = new HashMap<String, Object>();
        List<News> newses=newsService.findNewsByState(1,7);
        if(newses!=null){
            for(int i=0;i<newses.size();i++){
                String time= DateFormat.getDateInstance().format(newses.get(i).getTime());
                newses.get(i).setTimeA(time);
            }
            map.put("newses",newses);
        }else {
            map.put("newsesState",0);
        }
        return map;
}


    /**
     * 查找公告信息
     * @return
     */
    @RequestMapping(value = "/findNotice",method = RequestMethod.GET)
    public Map<String,Object> findNotice(){

        Map<String,Object> map = new HashMap<String, Object>();
        List<News> notices=newsService.findNewsByState(2,16);
        if(notices!=null){
            for(int i=0;i<notices.size();i++){
                String time= DateFormat.getDateInstance().format(notices.get(i).getTime());
                notices.get(i).setTimeA(time);
            }
            map.put("notices",notices);
        }else {
            map.put("noticesState",0);
        }
        return map;
    }


    /**
     * 添加公告
     * @param news1
     * @return
     */
    @RequestMapping(value = "/addNotice",method = RequestMethod.POST)
    public int addNotice(@RequestBody News news1){

        int result=0;
        if(news1!=null){
            news1.setState(2);
            news1.setTime(new Date());
            int newsId=newsService.addNews(news1);
            result=newsId;
        }else {
            result=0;
        }
        return result;
    }


    /**
     * 添加新闻
     * @param news2
     * @return
     */
    @RequestMapping(value = "/addNews",method = RequestMethod.POST)
    public int addNews(@RequestBody News news2){

        int result=0;
        if(news2!=null){
            news2.setState(1);
            news2.setTime(new Date());
            int newsId=newsService.addNews(news2);
            result=newsId;
        }else {
            result=0;
        }
        return result;
    }


    /**
     * 查询所有新闻
     * @param currPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/queryAllNews", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse queryAllNews(@RequestParam Integer currPage,
                                     @RequestParam Integer pageSize){
        ResponseBuilder custom = ResponseBuilder.custom();
        List<News> news=newsMapper.queryAllNews(1);
        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            if(news!=null){
                for(int i=0;i<news.size();i++){
                    String time= DateFormat.getDateInstance().format(news.get(i).getTime());
                    news.get(i).setTimeA(time);
                }
                custom.data(news).currPage(currPage).pageSize(pageSize).totalCount(news.size());
            }else {
                custom.failed("查询失败", -1);
            }
        } catch (Exception e) {
            custom.failed("查询失败", -1);
        }
        return custom.build();
    }

    /**
     * 查询所有公告
     * @param currPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/queryAllNotice", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse queryAllNotice(@RequestParam Integer currPage,
                                     @RequestParam Integer pageSize){
        ResponseBuilder custom = ResponseBuilder.custom();
        List<News> news=newsMapper.queryAllNews(2);
        if (Objects.isNull(currPage) || currPage == 0) currPage = 1;
        if (Objects.isNull(pageSize) || pageSize == 0) pageSize = 10;
        //分页
        PageHelper.startPage(currPage, pageSize);

        try {
            if(news!=null){
                for(int i=0;i<news.size();i++){
                    String time= DateFormat.getDateInstance().format(news.get(i).getTime());
                    news.get(i).setTimeA(time);
                }
                custom.data(news).currPage(currPage).pageSize(pageSize).totalCount(news.size());
            }else {
                custom.failed("查询失败", -1);
            }
        } catch (Exception e) {
            custom.failed("查询失败", -1);
        }
        return custom.build();
    }

    @RequestMapping(value = "/delNews", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse delIdea(Integer newsId){
        ResponseBuilder custom = ResponseBuilder.custom();
        int result=newsMapper.deleteByPrimaryKey(newsId);

        if (result!=0){
            custom.success("成功",0);
        }else{
            custom.failed("失败", -1);
        }
        return custom.build();
    }
}
